﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using YArchitech.Revit;

namespace EditColumn.ConnectColumnBream
{
	internal class UpdataDetailCurve
	{
		public static void UpdataDeleteDetail(List<ConnectColumnBreamInfo> lstUpdataInfo)
		{
			if (lstUpdataInfo.Count == 0)
			{
				return;
			}
			Document document = lstUpdataInfo[0].ConnectBreamInfo.ThisExternalCommandData.Application.ActiveUIDocument.Document;
			Transaction transaction = new Transaction(document, "UpdataDetail");
			try
			{
				transaction.Start();
				foreach (ConnectColumnBreamInfo connectColumnBreamInfo in lstUpdataInfo)
				{
					if (connectColumnBreamInfo.ArrayDetailCurve != null)
					{
						for (int i = 0; i < connectColumnBreamInfo.ArrayDetailCurve.Length; i++)
						{
							document.Delete(connectColumnBreamInfo.ArrayDetailCurve[i].Id);
							connectColumnBreamInfo.ArrayDetailCurve[i] = null;
						}
						connectColumnBreamInfo.ArrayDetailCurve = null;
					}
				}
				transaction.Commit();
			}
			catch
			{
				transaction.RollBack();
			}
		}

		public static void UpdataAddDetail(List<ConnectColumnBreamInfo> lstUpdataInfo)
		{
			if (lstUpdataInfo.Count == 0)
			{
				return;
			}
			Document document = lstUpdataInfo[0].ConnectBreamInfo.ThisExternalCommandData.Application.ActiveUIDocument.Document;
			Transaction transaction = new Transaction(document, "UpdataDetail");
			try
			{
				transaction.Start();
				foreach (ConnectColumnBreamInfo connectColumnBreamInfo in lstUpdataInfo)
				{
					if (connectColumnBreamInfo.ArrayDetailCurve == null)
					{
						DetailCurve[] array = new DetailCurve[4];
						for (int i = 0; i < connectColumnBreamInfo.TagLine.Length; i++)
						{
							DetailCurve detailCurve = document.Create.NewDetailCurve(connectColumnBreamInfo.ConnectBreamInfo.BreamViewPlan, connectColumnBreamInfo.TagLine[i]);
							Color color = new Color(byte.MaxValue, 0, 0);
							List<ElementId> list = new List<ElementId>();
							list.Add(detailCurve.Id);
							connectColumnBreamInfo.ConnectBreamInfo.BreamViewPlan.SetYJKProjColorOverrideByElement(list, color);
							array[i] = detailCurve;
						}
						connectColumnBreamInfo.ArrayDetailCurve = array;
					}
				}
				transaction.Commit();
			}
			catch
			{
				transaction.RollBack();
			}
		}
	}
}
